﻿using GBT.ParamEntity;
using GBT.SystemBusinessCore;
using GBT.SystemEnties;
using NLog;
using QueryDataLogic.Core;
using System;
using System.Linq;

namespace PersistenceDataLogic.Core
{
    public class OpterationUserStoredValue
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        BasicInfo bi = new BasicInfo();
        DbContext<UserStoredValue> dbContext = null;

        /// <summary>
        /// 判断用户充值账户信息是否存在
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        private bool IsUserStoredValue(int? UserID, DbContext<UserStoredValue> dbContext)
        {
            return dbContext.IsExists(t => t.UserID == UserID);
        }

        /// <summary>
        /// 用户充值操作
        /// </summary>
        /// <param name="userStoredValue"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int OptUserStoredValue(System_UserStoredValue userStoredValue, ref string msg)
        {
            int result = 0;
            var dbName = bi.GetDBName((int)userStoredValue.DistrictID);
            dbContext = new DbContext<UserStoredValue>(dbName);
            var db = dbContext.Db;
            try
            {
                if (userStoredValue.Value <= 0)
                {
                    msg = "充值金额不能为零";
                    return 0;
                }

                db.BeginTran();
                UserStoredValueRecord userStoredValueRecord = new UserStoredValueRecord();
                UserStoredValue usvNew = new UserStoredValue();
                //存在账户余额资料 则修改账户余额，否则新增账户余额资料
                if (IsUserStoredValue(userStoredValue.UserID, dbContext))
                {
                    var usv = GetUserValue(userStoredValue.UserID, dbContext);
                    usvNew.ID = usv == null ? 0 : usv.ID;
                    usvNew.UserID = (int)userStoredValue.UserID;
                    usvNew.Value = (usv == null ? 0 : usv.Value) + userStoredValue.Value;
                    usvNew.CreationDate = usv.CreationDate;
                    result += db.Updateable(usvNew).ExecuteCommand();
                }
                else
                {
                    usvNew.UserID = (int)userStoredValue.UserID;
                    usvNew.Value = userStoredValue.Value;
                    usvNew.CreationDate = DateTime.Now;
                    result = db.Insertable(usvNew).ExecuteCommand();
                }

                //添加充值记录
                userStoredValueRecord.UserID = (int)userStoredValue.UserID;
                userStoredValueRecord.Value = userStoredValue.Value;
                userStoredValueRecord.UserValue = usvNew.Value;
                userStoredValueRecord.Remark = userStoredValue.Remark;
                userStoredValueRecord.CreationDate = DateTime.Now;
                userStoredValueRecord.Creator = userStoredValue.Creator;
                result += db.Insertable(userStoredValueRecord).ExecuteCommand();
                db.CommitTran();
                msg = "用户充值成功";
            }
            catch (Exception ex)
            {
                db.RollbackTran();
                logger.Error(ex);
                Console.WriteLine(ex);
                msg = "用户充值失败";
            }
            return result;
        }

        /// <summary>
        /// 获取账户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        private UserStoredValue GetUserValue(int? userID, DbContext<UserStoredValue> dbContext)
        {
            var userStoredValue = dbContext.GetList(t => t.UserID == userID)?.FirstOrDefault();
            return userStoredValue;
        }

        /// <summary>
        /// 是否存在 取消充值 记录
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="dbContext"></param>
        /// <returns></returns>
        private bool IsUndoReload(int? ID, DbContext<UserStoredValueRecord> dbContext)
        {
            return dbContext.IsExists(t => t.Remark.Contains(ID.ToString()));
        }

        /// <summary>
        /// 取消充值
        /// </summary>
        /// <param name="userStoredValueRecord"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public int UndoReload(System_UserStoredValueRecord userStoredValueRecord, ref string msg)
        {
            int result = 0;
            var dbName = bi.GetDBName((int)userStoredValueRecord.DistrictID);
            dbContext = new DbContext<UserStoredValue>(dbName);
            var db = dbContext.Db;
            try
            {
                if (userStoredValueRecord.ID == null)
                {
                    msg = "充值记录不存在";
                    return 0;
                }
                var userStoredValue = GetUserValue(userStoredValueRecord.UserID, dbContext);
                if (userStoredValue == null || userStoredValue.Value < userStoredValueRecord.Value)
                {
                    msg = "账户余额不足";
                    return 0;
                }
                DbContext<UserStoredValueRecord> record = new DbContext<UserStoredValueRecord>(dbName);
                //判断是否已取消充值
                if (IsUndoReload(userStoredValueRecord.ID, record))
                {
                    msg = "已取消充值，无法重复取消充值";
                    return 0;
                }

                UserStoredValueRecord usvr = new UserStoredValueRecord();
                usvr.UserID = (int)userStoredValueRecord.UserID;
                usvr.Value = 0 - userStoredValueRecord.Value;
                usvr.Remark = "取消充值, 充值记录ID：" + userStoredValueRecord.ID;
                usvr.CreationDate = DateTime.Now;
                usvr.Creator = userStoredValueRecord.Creator;
                db.BeginTran();
                //修改账户余额
                userStoredValue.Value = userStoredValue.Value - userStoredValueRecord.Value;
                result += db.Updateable(userStoredValue).ExecuteCommand();
                //新增取消充值记录
                usvr.UserValue = userStoredValue.Value;
                result += db.Insertable(usvr).ExecuteCommand();
                db.CommitTran();
                msg = "取消充值成功";
            }
            catch (Exception ex)
            {
                db.RollbackTran();
                logger.Error(ex);
                Console.WriteLine(ex);
                msg = "取消充值失败";
            }
            return result;
        }
    }
}
